/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servletpackage;




import datapackage.IntituleProf;
import datapackage.IntituleProfBean;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



/**
 *
 * @author salim
 */
public class cleanServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, NamingException, SQLException {
        //les variables pour inserer un horaire hebdomadaire, ce sont recuperer ici
        //initialization des varaibles

        //int IdSalle =1;//la salle n'est pas des maint un cle
       

       
        String address; //l'adresse du page jsp a "forward"
        int noenseig=0;

        System.out.println("+++++++@ cleanservlet+++debut....................");
        //read the parameter submitted by the form middle
        // cell valeur pour la periode 1 :11,12;13,14,.....
     
            String cell = request.getParameter("cell");
            System.out.println("+++++++@ cleanservlet+++the passed parameter cell :" + cell);
//+++++++@ cleanservlet+++the passed parameter cell :34
                        //avoir le noenseig du request bean id
            HttpSession session=request.getSession();
            IntituleProfBean ipb=(IntituleProfBean) session.getAttribute("ipb");
           // IntituleProfBean ipb=(IntituleProfBean)request.getAttribute("ipb");
            IntituleProf ip=ipb.getIp();
           //cette if est inserer pour eviter le null pointer qd on click une cellule sans choisir un noenseig
            if(ip!=null){
            noenseig=ip.getNoenseig();
 System.out.println("+++++++@ cleanservlet+++le noenseig passer du request noenseig=" +noenseig );


            //error cant convert
            //      int cellint=Integer.getInteger(request.getParameter("cell"));
            //System.out.println("+++++++@ cleanservlet+++the passed parameter cellint :"+cellint);
            String j=cell.substring(1, 2);
            String p=cell.substring(0, 1);
            int Jour=Integer.parseInt(j);
            int PeriodeH=Integer.parseInt(p);

System.out.println("jour="+Jour+"periode="+PeriodeH+"cell="+Integer.parseInt(cell));


                if (datapackage.DivertList.SiHoraireExiste(noenseig,Jour,PeriodeH)) {
                    //si oui- cad le user veut supprimer un horaire deja existant
                    datapackage.DivertList.DeleteHorairehebdo2(noenseig,Jour, PeriodeH);
               } //si nom le user va ajouter un nouveau horaire
                else {
                   datapackage.DivertList.InsertHorairehebdo2(noenseig, Jour, PeriodeH);
                }

           



           }//fin de if ip.getnoenseig not null

      
        address = "/cleanpage.jsp";

        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            try {
                processRequest(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(cleanServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (NamingException ex) {
            Logger.getLogger(cleanServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            try {
                processRequest(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(cleanServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (NamingException ex) {
            Logger.getLogger(cleanServlet.class.getName()).log(Level.SEVERE, null, ex);
        }



    }

    /**
     * Returns a short description of the servlet.
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
